package com.kgc.onlineLife.controller;

import com.kgc.onlineLife.bean.User;
import com.kgc.onlineLife.service.UserService;
import com.kgc.onlineLife.util.EncodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * Created On : 2022/4/19.
 * <p>
 * Author     : nainai
 * <p>
 * Description: UserController
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * @author : nainai
     * @date   : 2022/4/18
     * @param  : [com.kgc.onlineLife.bean.Customer]
     * @return : java.lang.String
     * @description : 跳转至修改管理员信息页面
     */
    @RequestMapping("/toModUserInfo")
    public String toModUserInfo(Map<String, Object> map, HttpSession session){
        map.put("user", (User) session.getAttribute("loginAdmin"));

        return "/Admin/userInfo";
    }

    /**
     * @author : nainai
     * @date   : 2022/4/18
     * @param  : [com.kgc.onlineLife.bean.User]
     * @return : java.lang.String
     * @description : 修改管理员信息
     */
    @RequestMapping("/modUserInfo")
    public String modUserInfo(User user, Map<String, Object> map, HttpSession session){
        if (userService.modUserById(user)){
            user.setPassword(((User) session.getAttribute("loginAdmin")).getPassword());
            session.setAttribute("loginAdmin", user);
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }
        return "/Admin/index";
    }

    /**
     * @author : nainai
     * @date   : 2022/4/18
     * @param  : [com.kgc.onlineLife.bean.User]
     * @return : java.lang.String
     * @description : 修改管理员密码
     */
    @RequestMapping("/modUserPassword")
    public String modUserPassword(HttpSession session, HttpServletResponse response, Map<String, Object> map, @RequestParam("oldpwd") String oldpwd, @RequestParam("newpwd") String newpwd, @RequestParam("confirmpwd") String confirmpwd) throws IOException {
        // 获取当前登录管理员信息
        User user1 = (User) session.getAttribute("loginAdmin");
        oldpwd = EncodeUtils.testDigest(oldpwd);

        if (oldpwd.equals(user1.getPassword()) && newpwd.equals(confirmpwd)) {
            // 调用修改方法
            User user = new User();
            user.setId(user1.getId());
            user.setPassword(EncodeUtils.testDigest(newpwd));
            userService.modUserById(user);
            map.put("changeFlag", true);
        } else {
            if (!oldpwd.equals(user1.getPassword())){
                map.put("oldpwdMsg", "*旧密码输入有误");
            } else if (!newpwd.equals(confirmpwd)){
                map.put("newpwdMsg", "*两次输入密码不一致");
            }
        }
        return "/Admin/changePwd";
    }

}
